package com.cn.jx.configuration;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.cn.jx.manager.po.SystemConfig;
import com.cn.jx.manager.service.mtn.ICityServiceMtn;
import com.cn.jx.manager.service.mtn.IDataGridServiceMtn;
import com.cn.jx.manager.service.mtn.IDictServiceMtn;
import com.cn.jx.manager.service.mtn.IResourceServiceMtn;
import com.cn.jx.manager.service.mtn.ISystemInfoServiceMtn;
import com.cn.jx.manager.service.query.ICityServiceQry;
import com.cn.jx.manager.service.query.IResourceServiceQry;
import com.cn.jx.manager.service.query.ISystemInfoServiceQry;

@Component
public class StartRunner implements CommandLineRunner {

	private static Logger log = LoggerFactory.getLogger("exception");
	
	@Autowired
	private IResourceServiceQry resourceServiceQry;
	@Autowired
	private IResourceServiceMtn resourceServiceMtn;
	@Autowired
	private ICityServiceQry cityServiceQry;
	@Autowired
	private ICityServiceMtn cityServiceMtn;
	@Autowired
	private IDictServiceMtn dictServiceMtn;
	@Autowired
	private IDataGridServiceMtn dataGridServiceMtn;
	@Autowired
	private ISystemInfoServiceQry systemInfoServiceQry;
	@Autowired
	private ISystemInfoServiceMtn systemInfoServiceMtn;
	
	@Override
	public void run(String... args) throws Exception {
		log.info("服务器启动成功 开始检测...");
		//初始化资源
		//说明第一次登录 系统 初始化
		log.info("系统正在初始化资源！");
		List<String> codes=resourceServiceQry.findAllResourceCode();
		resourceServiceMtn.saveResourceInit("ROOT", "ROOT",codes);
		//初始化城市
		int city=cityServiceQry.findAllCityCount();
		if(city==0){
			log.info("检测到城市数据不存在，系统正在初始化城市！");
			cityServiceMtn.saveCityInit();
		}
		//初始化字典
		log.info("系统正在初始化化字典！");
		dictServiceMtn.saveDictInit();
		//初始化动态列
		log.info("系统正在初始化动态列！");
		dataGridServiceMtn.initDataGrid();
		//初始化系统配置
		SystemConfig fig=systemInfoServiceQry.findSystemConfig();
		if(fig==null){
			log.info("检测到系统配置，系统正在初始化配置！");
			systemInfoServiceMtn.initSystemConfig();
		}
		log.info("检测完毕,正常使用");
	}
}
